<?php

class DocumentController extends \BaseController {

	/**
	 * 获取内容列表
	 * @param  [id] $categoryId [栏目id]
	 * @return [array]             [栏目id、栏目名、文档内容]
	 */
	public function index(){
		$categoryId = Input::get('categoryId');
		$data		= array('categoryId' => $categoryId);
		$rules		= array('categoryId' => 'required | exists:category,id');
		$validator 	= CustomValidator::validateInput($data, $rules);
		if(isset($validator)){
			return Response::json($validator);
		}
		$documents = DocumentService::index($categoryId);
		// dd($documents);
		return View::make('document.index')->with('documents', $documents);
	}

	/**
	 *  进入文档增加页面
	 * @param  [int] $categoryId [栏目id]
	 * @return [array]             [栏目信息，模板字段信息]
	 */
	public function create(){
		$categoryId = Input::get('categoryId');
		$data		= array('categoryId' => $categoryId);
		$rules		= array('categoryId' => 'required | exists:category,id');
		$validator 	= CustomValidator::validateInput($data, $rules);
		if(isset($validator)){
			return Response::json($validator);
		}
		$document = DocumentService::create($categoryId);
		return View::make('document.add')->with('document', $document);
	}

	/**
	 * 增加文档
	 * @param [idt] $categoryId [栏目id]
	 */
	public function add(){
		$categoryId = Input::get('categoryId');
		$data		= array('categoryId' => $categoryId);
		$rules		= array('categoryId' => 'required | exists:category,id');
		$validator 	= CustomValidator::validateInput($data, $rules);
		if(isset($validator)){
			return Response::json($validator);
		}
		$message = DocumentService::add($categoryId);
		return Response::json($message);
	}
	
    /**
     * 显示文档详情
     * @return [文档详情
     * 
     */
	public function show(){
		$id         = Input::get('id');
		$categoryId = Input::get('categoryId');
		$data       = array('categoryId' 	=> $categoryId, 'id' => $id);
		$rules      = array('categoryId' 	=> 'required | exists:category,id',
							 'id' 			=> 'required | integer');
		$validator  = CustomValidator::validateInput($data, $rules);
		if(isset($validator)){
			return Response::json($validator);
		}

		$documents = DocumentService::show($id, $categoryId);
		return View::make('document.show')->with('documents', $documents);
	}

	/**
	 * 进入文档更改页面
	 * @param  [int] $id         [文档id]
	 * @param  [int] $categoryId [栏目id]
	 * @return [array]             [文档信息，模板字段]
	 */
	public function edit(){
		$id         = Input::get('id');
		$categoryId = Input::get('categoryId');
		$data       = array('categoryId' 	=> $categoryId, 'id' => $id);
		$rules      = array('categoryId' 	=> 'required | exists:category,id',
							 'id' 			=> 'required | integer');
		$validator  = CustomValidator::validateInput($data, $rules);
		if(isset($validator)){
			return Response::json($validator);
		}
		$documents = DocumentService::show($id, $categoryId);
		return View::make('document.update')->with('documents', $documents);
	}

	/**
	 * 更新文档数据
	 * @param  [int] $id         [文档id]
	 * @param  [int] $categoryId [栏目id]
	 * @return []             []
	 */
	public function update(){
		$id         = Input::get('id');
		$categoryId = Input::get('categoryId');
		$data       = array('categoryId' => $categoryId, 'id' => $id);
		$rules      = array('categoryId' => 'required | exists:category,id',
							 'id'        => 'required | integer');
		$validator  = CustomValidator::validateInput($data, $rules);
		if(isset($validator)){
			return Response::json($validator);
		}
		$message = DocumentService::update($id, $categoryId);
		return Response::json($message);
	}

	/**
	 * 删除文档
	 * @param  [int] $id         [文档id]
	 * @param  [int] $categoryId [栏目id]
	 */
	public function delete(){
		$id         = Input::get('id');
		$categoryId = Input::get('categoryId');
		$data       = array('categoryId' => $categoryId, 'id' => $id);
		$rules      = array('categoryId' => 'required | exists:category,id',
							 'id'        => 'required | integer');
		$validator  = CustomValidator::validateInput($data, $rules);
		if(isset($validator)){
			return Response::json($validator);
		}
		$message = DocumentService::delete($id, $categoryId);

		return Response::json($message);
	}

}